package ligne2vie.topos.shared.bo;

import java.io.Serializable;
import java.util.List;

import com.google.gwt.maps.client.geocoder.HasAddressComponent;

import ligne2vie.topos.client.maps.MyMapWidget;
import ligne2vie.topos.client.maps.proxy.MyMarker;

/**
 * Data Transfert Object from BO to Client.
 * 
 * @author thuguerre
 * @since 1.1.0
 */
public class TGeorequestResult extends TMapPoint implements Serializable {

	/**
	 * Default serial UID.
	 */
	private static final long serialVersionUID = -7069158728316219423L;

	/**
	 * This attribute contains the index of the result inside its result list.
	 * 
	 * @since 1.1.0
	 */
	protected long resultIndex;

	/**
	 * Default constructor. Required to implement {@link Serializable}.
	 */
	protected TGeorequestResult() {
	}

	/**
	 * Specialized constructor.
	 * 
	 * @param resultIndex
	 *            Index of the result in the list of results it is extracted from. This has an impact on the displayed
	 *            marker.
	 * @param latitude
	 *            Result's latitude.
	 * @param longitude
	 *            Result's longitude.
	 * @param addressComponents
	 *            Result address components, used to display on the map.
	 */
	public TGeorequestResult(long resultIndex, double latitude, double longitude,
			List<HasAddressComponent> addressComponents) {

		super();

		this.resultIndex = resultIndex;
		this.latitude = latitude;
		this.longitude = longitude;

		// --- preparing description which corresponds to the result of the method getInfoWindowContent

		this.description = AddressHelper.getHtmlFormatedAddressBlock(addressComponents);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getInfoWindowContent() {

		return getDescription();
	}

	/**
	 * {@inheritDoc}
	 */
	public MyMarker getMarker() {

		if (marker == null) {

			marker = MyMarker.build(getIconPath());

			marker.setPosition(getPosition());
			marker.setMap(MyMapWidget.getInstance().getMap());
		}

		return marker;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getIconPath() {

		if (this.resultIndex == -1) {
			return "/img/maps/arrow/arrow.png";
		} else {
			return "/img/maps/marker-letters/yellow_Marker" + ((char) (65 + this.resultIndex)) + ".png";
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getShortDescription() {

		return getDescription();
	}
}
